package com.ebt.m.repository.provider;

import com.ebt.m.data.bean.EbtFile;

import java.util.Comparator;
import java.util.HashMap;

/**
 * ClassName: FileSortHelper <br/>
 * Function: 文件排序 <br/>
 * 
 * @author damon.han <br/>
 * @date 2015年5月14日 上午11:53:22 <br/>
 */
public class FileSortHelper {

	public enum SortMethod {
		name, type, size, date
	}

	private SortMethod mSort;

	private boolean mFileFirst;

	private HashMap<SortMethod, Comparator> mComparatorList = new HashMap<SortMethod, Comparator>();

	public FileSortHelper() {
		mSort = SortMethod.name;
		mComparatorList.put(SortMethod.name, cmpName);
		mComparatorList.put(SortMethod.size, cmpSize);
		mComparatorList.put(SortMethod.date, cmpDate);
		mComparatorList.put(SortMethod.type, cmpType);
	}

	public void setSortMethog(SortMethod s) {
		mSort = s;
	}

	public SortMethod getSortMethod() {
		return mSort;
	}

	public void setFileFirst(boolean f) {
		mFileFirst = f;
	}

	public Comparator getComparator() {
		return mComparatorList.get(mSort);
	}

	private abstract class FileComparator implements Comparator<EbtFile> {

		@Override
		public int compare(EbtFile object1, EbtFile object2) {
			if (object1 == null || object2 == null) {
				return 0;
			}
			if (object1.isFolder == object2.isFolder) {
				return doCompare(object1, object2);
			}

			if (mFileFirst) {
				// the files are listed before the dirs
				return (object1.isFolder ? 1 : -1);
			} else {
				// the dir-s are listed before the files
				return object1.isFolder ? -1 : 1;
			}
		}

		protected abstract int doCompare(EbtFile object1, EbtFile object2);
	}

	private Comparator cmpName = new FileComparator() {
		@Override
		public int doCompare(EbtFile object1, EbtFile object2) {
			if (object1.name == null || object2.name == null) {
				return 0;
			}
			return object1.name.compareToIgnoreCase(object2.name);
		}
	};

	private Comparator cmpSize = new FileComparator() {
		@Override
		public int doCompare(EbtFile object1, EbtFile object2) {
			return longToCompareInt(object1.size - object2.size);
		}
	};

	private Comparator cmpDate = new FileComparator() {
		@Override
		public int doCompare(EbtFile object1, EbtFile object2) {
			if (object1 == null || object2 == null) {
				return 0;
			}
			if (object1.lastModify == object2.lastModify) {
				return 0;
			}
			return longToCompareInt(object2.lastModify - object1.lastModify);
		}
	};

	private int longToCompareInt(long result) {
		return result > 0 ? 1 : (result < 0 ? -1 : 0);
	}

	private Comparator cmpType = new FileComparator() {
		@Override
		public int doCompare(EbtFile object1, EbtFile object2) {
			int result = Util.getExtFromFilename(object1.name).compareToIgnoreCase(Util.getExtFromFilename(object2.name));
			if (result != 0)
				return result;

			return Util.getNameFromFilename(object1.name).compareToIgnoreCase(Util.getNameFromFilename(object2.name));
		}
	};
}
